package com.cll.service.impl;

import com.cll.context.BaseContext;
import com.cll.dto.ShoppingCartDTO;
import com.cll.entity.Dish;
import com.cll.entity.Setmeal;
import com.cll.entity.ShoppingCart;
import com.cll.mapper.DishMapper;
import com.cll.mapper.SetmealMapper;
import com.cll.mapper.ShoppingCartMapper;
import com.cll.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        //目标：添加购物车，如果商品在购物车中已存在更新购物数量+1，如果不存在就添加新的购物车记录数据

        //1.将shoppingCartDTO封装给实体类对象 shoppingCart
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        //2.shoppingCart封装user_id  因为用户只能查看自己的购物车,所以是必须封装到user_id
        //设置查询条件
        //  分析；user_id: BaseContext.getCurrentId()
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //3.查询user_id，dish_id，setmeal_id，dish_flavor条件查询用户的购物车记录数据 dbShoppingCart
        //  sql分析：根据select * from shopping_cart where user_id=xx and dish_id=xxx and setmeal_id=xx and dish_flavor=xxx
        //通俗易懂点    调用mapper层查看当前商品是否在购物车中
        ShoppingCart dbShoppingCart = shoppingCartMapper.find(shoppingCart);

        if (dbShoppingCart != null) {
            //4.dbShoppingCart 不为null  如果购物车里面是有菜品或者套餐的时候,选择相同的则是+1
            //更新购物车对象数量，shoppingCartMapper.update(shoppingCart);
            //sql分析：update shopping_cart set number=#{number} where id=#{id}
            dbShoppingCart.setNumber(dbShoppingCart.getNumber() + 1);
            shoppingCartMapper.updateCount(dbShoppingCart);
        } else {
            //5.dbShoppingCart 为null  如果购物车里面是没有菜品或者套餐的时候,当买到菜品的时候就会获取菜品的名称，图片，单价 前提是根据菜品的id去查询的
            //5.1 获取当前商品的名称，图片，单价
            //    如果买的菜品：name,image,amount 根据dishId查询菜品就可以获取
            if (shoppingCart.getDishId() != null) {
                Dish dishMapperById = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dishMapperById.getName());
                shoppingCart.setImage(dishMapperById.getImage());
                shoppingCart.setAmount(dishMapperById.getPrice());
            }

            //    如果买的套餐：name,image,amount 根据setmeal_id查询菜品就可以获取
            if (shoppingCart.getSetmealId() != null) {
                Setmeal setmealMapperById = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmealMapperById.getName());
                shoppingCart.setImage(setmealMapperById.getImage());
                shoppingCart.setAmount(setmealMapperById.getPrice());
            }

            //5.2 添加购物车
            //添加购物车数据，shoppingCartMapper.add(shoppingCart); 每一次购买数量默认=1
            shoppingCart.setNumber(1);
            //添加进购物车的时间
            shoppingCart.setCreateTime(LocalDateTime.now());

            //分析：insert into shopping_cart(name,image,user_id,dish_id,setmeal_id,dish_flavor,amount)
            //用户选好的外卖,都是会先加入短暂的购物车中存储,一旦支付了,购物车就会清空 所以这里需要调用shoppingCartMapper弄一个添加购物车的方法
            shoppingCartMapper.save(shoppingCart);
        }


    }

    /**
     * 查询获取购物车里面的集合
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.list(userId);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }


    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //1.将shoppingCartDTO封装给实体类对象 shoppingCart
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        //2.因为用户只能查看自己的购物车 设置查询条件
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //3.调mapper看购物车是否有数据
        ShoppingCart dbShoppingCart = shoppingCartMapper.find(shoppingCart);

        //4.不能平白无故的删除,先判断购物车中所需要删掉的商品是否存在或者数量是否>0
        //4.1如果购物车的某一个物品 ==1,不想买的话 -1就是直接删除
        if (dbShoppingCart.getNumber() == 1) {
            //更新购物车对象数量，shoppingCartMapper.update(shoppingCart);
            //sql分析：update shopping_cart set number=#{number} where id=#{id}
            shoppingCartMapper.deleteCount(dbShoppingCart);
        } else {
            //4.2如果购物车的某一个物品 >1,不想买多的话 -1就是修改
            //更新购物车对象数量，shoppingCartMapper.update(shoppingCart);
            //sql分析：update shopping_cart set number=#{number} where id=#{id}
            dbShoppingCart.setNumber(dbShoppingCart.getNumber() -1);
            shoppingCartMapper.updateCount(dbShoppingCart);
        }
    }
}